<?php
/**
 * Created by PhpStorm.
 * User: yangliu
 * Date: 2019-01-09
 * Time: 11:03
 */

namespace app\common\model\order;

use app\admin\model\AuthDepartment;
use app\admin\model\Department;
use app\common\model\Area;
use think\Model;

/**
 * 地区数据模型
 */
class Order extends Model
{
    protected $name = 'order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'depid_text',
        'status_text',
        'deliverystatus_text',
        'pay_text'
    ];


    public function getStatusList()
    {
        return [-1=>'退回修改',0=>'保存中',1=>'审核中',2=>'审核通过',8=>'订单作废'];
    }

    public function getPayList()
    {
        return [1=>'已付款',2=>'未结清',3=>'未付款',4=>'待确认'];
    }

    public function getPayTextAttr($value, $data)
    {
        $value = $value ? $value : $data['pay'];
        $list = $this->getPayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getDeliverystatusList()
    {
        //审核完成 直接发货
        return [-1=>'订单已作废',0=>'待发货',1=>'已发货',2=>'发货未完成',3=>'已完成'];
    }
    public function getDeliverystatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['deliverystatus'];
        $list = $this->getDeliverystatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getDepidList()
    {
        return AuthDepartment::column('id,name');
    }

    public function getDepidTextAttr($value, $data)
    {
        $value = $value ? $value : $data['dep_id'];
        $list = $this->getDepidList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function githouse()
    {
        return $this->belongsTo('app\admin\model\Githouse','githouse_id','id',[],'LEFT');
    }

    public function orderlog()
    {
        return $this->hasMany('app\common\model\order\Orderlog','order_id');
    }

    public function orderItem()
    {
        return $this->hasMany('app\common\model\order\Orderitem','order_id');
    }

    public function item()
    {
        return $this->belongsTo('app\common\model\order\Orderitem', 'id', 'order_id', [], 'LEFT')->setEagerlyType(0);
    }

    public function log()
    {
        return $this->belongsTo('app\common\model\order\Orderlog', 'id', 'order_id', [], 'LEFT')->setEagerlyType(0);
    }

}
